package api

import (
	"fmt"
	"ims/app/dao"
	"ims/app/model"
	"ims/tools"
	"ims/tools/response"
	"math/rand"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

func PostVisitorLogin(r *ghttp.Request) {
	ipcity := tools.ParseIp(r.GetClientIp())
	avator := fmt.Sprintf("/static/images/%d.jpg", rand.Intn(14))
	toId := r.GetFormString("to_id")
	id := r.GetFormString("id")
	if id == "" {
		id = tools.Uuid()
	}
	refer := r.GetFormString("refer")
	var (
		city string
		name string
	)
	if ipcity != nil {
		city = ipcity.CountryName + ipcity.RegionName + ipcity.CityName
		name = ipcity.CountryName + ipcity.RegionName + ipcity.CityName + "网友"
	} else {
		city = "未识别地区"
		name = "匿名网友"
	}
	client_ip := r.GetFormString("client_ip")
	//client_ip := r.GetClientIp()
	//log.Println(name,avator,c.ClientIP(),toId,id,refer,city,client_ip)
	if name == "" || avator == "" || toId == "" || id == "" || refer == "" || city == "" {
		// response.JsonResponse(r, g.Map{
		// 	"code": 400,
		// 	"msg":  "error",
		// })
		// return
	}
	kefuInfo := dao.FindUser(toId)
	if kefuInfo.Id == 0 {
		response.JsonResponse(r, g.Map{
			"code": 400,
			"msg":  "客服不存在",
		})
		return
	}
	dao.CreateVisitor(name, avator, r.GetClientIp(), toId, id, refer, city, client_ip)
	visitor := dao.FindVisitor(id)
	response.JsonResponse(r, g.Map{
		"code":   200,
		"msg":    "ok",
		"result": visitor,
	})
}

func GetVisitors(r *ghttp.Request) {
	pagesize := uint(20)
	page := r.GetInt("page")
	kefuId := r.GetString("kefu_name")

	vistors := dao.FindVisitorsByKefuId(uint(page), pagesize, kefuId)
	count := dao.CountVisitorsByKefuId(kefuId)
	response.JsonResponse(r, g.Map{
		"code": 200,
		"msg":  "ok",
		"result": g.Map{
			"list":     vistors,
			"count":    count,
			"pagesize": pagesize,
		},
	})
}

func GetVisitor(r *ghttp.Request) {
	visitorId := r.GetQueryString("visitorId")

	vistor := dao.FindVisitor(visitorId)
	response.JsonResponse(r, g.Map{
		"code":   200,
		"msg":    "ok",
		"result": vistor,
	})
}

func GetVisitorMessage(r *ghttp.Request) {
	visitorId := r.GetQueryString("visitorId")
	messages := dao.FindMessageByVisitorId(visitorId)
	result := make([]map[string]interface{}, 0)
	for _, message := range messages {
		item := make(map[string]interface{})
		var visitor model.Visitor
		var kefu model.User

		if visitor.Name == "" || kefu.Name == "" {
			kefu = dao.FindUser(message.KefuId)
			visitor = dao.FindVisitorByVistorId(message.VisitorId)
		}
		item["time"] = message.CreatedAt.Format("Y-m-d H:i:s")
		item["content"] = message.Content
		item["mes_type"] = message.MesType
		item["visitor_name"] = visitor.Name
		item["visitor_avator"] = visitor.Avator
		item["kefu_name"] = kefu.Nickname
		item["kefu_avator"] = kefu.Avator
		result = append(result, item)

	}
	dao.ReadMessageByVisitorId(visitorId)
	response.JsonResponse(r, g.Map{
		"code":   200,
		"msg":    "ok",
		"result": result,
	})
}
